<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class CategoryController extends CoreController {
	public function Index(){
        $ComModel = M('Category');
        $ComList = $ComModel->order('level asc')->page($page.',20')->select();
        $this->assign('list',$ComList);
        $count = $ComModel->count();
        $Page = new \Think\Page($count,20);
        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('addText','添加分类');
        $this->assign('addNew',U('addCategory'));
        $this->assign('edit',U('editCategory'));
        $this->assign('del',U('delCategory'));
        $this->categoryForm=getCategoryList();
        $this->display();
       	return getCategoryForm();
       	return getCategoryPid();
	}

	public function addCategory(){
		
		//判断是否进行编辑
		// if (!empty($_GET['id'])) {
		// 	$id=$_GET['id'];
		// 	$row=M('Category')->where("id='{$id}'")->find();
		// }

		// if (isset($row['pid'])) {
			// 进入编辑的表单分类情况
			// $this->categoryForm=getCategoryForm($row['pid']);
		// }else{
			// 进入增加分类的情况
			$this->categoryForm=getCategoryForm();
		// }
		if (IS_POST) {
			$data=array(
					'cate_name'=>I('post.cate_name'),
					'cate_keywords'=>I('post.cate_keywords'),
					'cate_discription'=>I('post.cate_discription'),
					'pre_id'=>I('post.pre_id'),
					'cate_addtime'=>time(),
					'status'=>I('post.status'),
				);
			if ($data['pre_id']==0) {
				$data['level']=1;
			}else{
				$id=$data['pre_id'];
				$r=M("Category")->field('level')->where("id=".$id)->find();
				
				if ($r) {
					$data['level']=$r['level']+1;
				}
			}
			$cate=M('Category')->add($data);
			header('Location:'.U('index'));
		}else{
			$this->display();
		}
	}

	public function editCategory(){
		if(IS_POST){
    		$id = I('post.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
    		$data=array(
					'cate_name'=>I('post.cate_name'),
					'cate_keywords'=>I('post.cate_keywords'),
					'cate_discription'=>I('post.cate_discription'),
					'pre_id'=>I('post.pre_id'),
					'cate_addtime'=>time(),
					'status'=>I('post.status'),
				);
            
    		$status = M('Category')->where("id=".$id)->save($data);
			header('Location:'.U('index'));
    	}else{
    		$id = I('get.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }

            $userInfo = M('Category')->where('id='.$id)->find();
            $this->categoryForm=getCategoryForm($id);
	        $this->assign('id',$id);
	        $this->assign('userInfo',$userInfo);
	        $this->assign('action',U('editCategory'));
	        $this->assign('cancel',U('index'));

	        //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
	        $this->display();
    	}
	}

	public function delCategory(){
		$id=I('get.id',0,'intval');
		if ($id<1) {
			$this->error('错误的ID');
		}
		$category=M('Category')->where("id=".$id)->delete();
		$comcate=M('ComCate')->where("cate_id=".$id)->delete();
		header('location:'.U('index'));

	}
}